﻿#include "TableWidgetPropertyEditEntry.h"
#include <QTableWidget>

#include "IntegerEditEntry.h"

#include "ObjectPropertyEditEntryFactory.h"
REGISTER_PROPERTY_EDIT_ENTRY(TableWidget)

struct TableWidgetPropertyEditEntry::TableWidgetPropertyEditEntryPrivate {
    QPointer<QTableWidget> tableWidget;
    QPointer<IntegerEditEntry> entryRowCount;
    QPointer<IntegerEditEntry> entryColumnCount;
};

TableWidgetPropertyEditEntry::~TableWidgetPropertyEditEntry() { }

TableWidgetPropertyEditEntry::TableWidgetPropertyEditEntry(QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TableWidgetPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
}

TableWidgetPropertyEditEntry::TableWidgetPropertyEditEntry(void *object, QWidget *parent) : PropertyEditEntry{parent} {
    p.reset(new TableWidgetPropertyEditEntryPrivate);
    initUi();
    retranslateUi();
    setObject(object);
}

void TableWidgetPropertyEditEntry::setObject(void *object) {
    p->tableWidget = static_cast<QTableWidget*>(object);
}

void TableWidgetPropertyEditEntry::onTick() {
    if(!p->tableWidget || !isVisible()) {
        return ;
    }

    p->entryRowCount->setValue(p->tableWidget->rowCount());
    p->entryColumnCount->setValue(p->tableWidget->columnCount());
}

void TableWidgetPropertyEditEntry::initUi() {
    p->entryRowCount = new IntegerEditEntry;
    appendChildEntry(p->entryRowCount);

    p->entryColumnCount = new IntegerEditEntry;
    appendChildEntry(p->entryColumnCount);
}

void TableWidgetPropertyEditEntry::retranslateUi() {
    setTitle(tr("QTableWidget"));
    p->entryRowCount->setTitle(tr("rowCount"));
    p->entryColumnCount->setTitle(tr("columnCount"));
}
